package com.example.uniapp.activity.userHome;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.uniapp.R;
import com.example.uniapp.activity.adapter.HomeCookbookListAdapter;
import com.example.uniapp.bean.CookbookBean;
import com.example.uniapp.bean.UserBean;
import com.example.uniapp.dao.CookbookDao;

import java.util.List;

public class CookbookUserHome extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cookbook_user_home);

        //首先获取各个组件
        Toolbar toolbar = this.findViewById(R.id.cookbook_user_home_back);
        ImageView userAvator = this.findViewById(R.id.cookbook_user_home_user_avator);
        TextView userName = this.findViewById(R.id.cookbook_user_home_user_name);
        ListView listView = this.findViewById(R.id.cookbook_user_home_list);

        //然后获取该人物信息
        Intent intent = getIntent();
        UserBean userBean = (UserBean) intent.getSerializableExtra("userInfo");

        //设置该人物信息到界面
        Bitmap userAvatorBitmap = BitmapFactory.decodeFile(userBean.getUserImg());
        userAvator.setImageBitmap(userAvatorBitmap);
        userName.setText(userBean.getUserId());

        //获取该人物发布的所有食谱并显示
        List<CookbookBean> cookbookList = CookbookDao.getCookbookListByUserId(userBean.getUserId());
        HomeCookbookListAdapter homeCookbookListAdapter = new HomeCookbookListAdapter(this, cookbookList);
        if(homeCookbookListAdapter.isEmpty()) {
            listView.setAdapter(null);
        }else {
            listView.setAdapter(homeCookbookListAdapter);
        }

        //返回功能
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //返回
                finish();
            }
        });
    }
}